package tp.pr4;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JApplet;
import javax.swing.JFrame;

import static tp.pr4.Constants.*;
import tp.pr4.cityLoader.CityLoaderFromTxtFile;
import tp.pr4.gui.MainWindow;

/**
 * 
 * 
 * @author Ricardo Eugui Fernandez
 * @version 1
 */
public class Main extends JApplet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unused")
	private static MainWindow gameWindow;

	public static void main(String[] args) {
		if (args.length == 0) {
			System.err.println(BAD_PARAMS + LINE_SEPARATOR);
			System.err.println(MAPFILE);
			System.exit(1);
		} else {
			try {
				InputStream file = new FileInputStream(args[0]);
				CityLoaderFromTxtFile fileLoader = new CityLoaderFromTxtFile();
				City city = fileLoader.loadCity(file);
				RobotEngine robot = new RobotEngine(city,
						fileLoader.getInitialPlace(), Direction.NORTH);
				gameWindow = new MainWindow(robot);
			} catch (FileNotFoundException e) {
				System.err
						.println(FILE_READ_ERROR + args[0] + FILE_READ_ERROR2);
				System.exit(2);
			} catch (IOException e) {
				System.err.println(FILE_FORMAT_ERROR);
				System.exit(2);
			}
		}
	}

	public void init(String[] args) {
		if (args.length == 0) {
			System.err.println(BAD_PARAMS + LINE_SEPARATOR);
			System.err.println(MAPFILE);
			System.exit(1);
		} else {
			try {
				InputStream file = new FileInputStream(args[0]);
				CityLoaderFromTxtFile fileLoader = new CityLoaderFromTxtFile();
				City city = fileLoader.loadCity(file);
				RobotEngine robot = new RobotEngine(city,
						fileLoader.getInitialPlace(), Direction.NORTH);
				gameWindow = new MainWindow(robot);
			} catch (FileNotFoundException e) {
				System.err
						.println(FILE_READ_ERROR + args[0] + FILE_READ_ERROR2);
				System.exit(2);
			} catch (IOException e) {
				System.err.println(FILE_FORMAT_ERROR);
				System.exit(2);
			}
		}
	}

}
